package com.liangzhong.modules.leagueMatch.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.LeagueMatchEvent;
	import com.liangzhong.events.MainEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.LeagueMatchType;
	import com.liangzhong.proxyconstant.leagueMatch.LeagueMatchConst;
	import com.lz.football.module.leagueMatch.ILeagueMatchView;
	
	import flash.display.DisplayObject;
	
	import lz.controls.Alert;
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *联赛 
	 * @author lake
	 * 
	 */	
	public class LeagueMatchMediator extends ModuleMediator
	{
		public static const MediatorName :String = "LeagueMatchMediator";
		/**
		 *请求参数 
		 */		
		private var reqParam :*;
		
		public function LeagueMatchMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取视图 
		 * @return 
		 * 
		 */		
		public function get view():ILeagueMatchView
		{
			return viewComponent as ILeagueMatchView;
		}
		/**
		 * 初始化
		 * 
		 */		
		override public function initMediator():void
		{
			addEventListeners();
		}
		/**
		 * 
		 * 添加事件
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).addEventListener(LeagueMatchEvent.PRE_MATCH_START,preMatchStartHandler);
			(view as DisplayObject).addEventListener(LeagueMatchEvent.BASE_MATCH_START,baseMatchStartHandler);
			(view as DisplayObject).addEventListener(LeagueMatchEvent.CHAMPION_MATCH_START,championMatchStartHandler);
			(view as DisplayObject).addEventListener(LeagueMatchEvent.LEGEND_MATCH_START,legendMatchStartHandler);
			(view as DisplayObject).addEventListener(LeagueMatchEvent.SIGN_STAR,signHandler);
		}
		/**
		 * 删除事件
		 * 
		 */		
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).removeEventListener(LeagueMatchEvent.PRE_MATCH_START,preMatchStartHandler);
			(view as DisplayObject).removeEventListener(LeagueMatchEvent.BASE_MATCH_START,baseMatchStartHandler);
			(view as DisplayObject).removeEventListener(LeagueMatchEvent.CHAMPION_MATCH_START,championMatchStartHandler);
			(view as DisplayObject).removeEventListener(LeagueMatchEvent.LEGEND_MATCH_START,legendMatchStartHandler);
			(view as DisplayObject).removeEventListener(LeagueMatchEvent.SIGN_STAR,signHandler);
		}
		/**
		 * 删除模块
		 * @param event
		 * 
		 */
		private function onRemoveModuleHandler(e:ModuleEvent):void
		{
			//通知离开比赛二级场景
			this.sendNotification(NotificationConstants.MATCH_LEAVE_SECOND_SCENE);
			removeMediator();
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			super.removeMediator();
			facade.removeMediator(MediatorName);
		}
		/**
		 *预选赛开始 
		 * @param e
		 * 
		 */		
		private function preMatchStartHandler(e :LeagueMatchEvent):void
		{
			this.sendNotification(ApplicationConstants.LEAGUEMATCH_PRE_START,e.param);
		}
		/**
		 *甲级赛开始 
		 * @param e
		 * 
		 */		
		private function baseMatchStartHandler(e :LeagueMatchEvent):void
		{
			if(AppData.getInstance().playerCenterDataManager.fieldPlayerinjuredNum)
			{
				Alert.show("您的上场球员已经有5名受伤，球员们联合起来罢赛了！" +
					"他们抗议如此赤果果的压榨行为并且上报国际足联，老板，赶快去给他们疗伤吧~",
					"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,openPlayerCenter);
				return;
			}
			reqParam = e.param;
			if(AppData.getInstance().playerCenterDataManager.fieldisinjured)
			{
				hospSure(baseMatchStart);
				return;
			}
			baseMatchStart();
		}
		/**
		 *甲级赛开始 
		 * @param flag
		 * 
		 */		
		private function baseMatchStart(flag :int = Alert.YES):void
		{
			if(flag == Alert.YES)
			{
				this.sendNotification(ApplicationConstants.LEAGUEMATCH_BASE_START,reqParam);
			}
			else
			{
				view.errorRes();
			}
		}
		/**
		 *欧冠赛开始 
		 * @param e
		 * 
		 */		
		private function championMatchStartHandler(e :LeagueMatchEvent):void
		{
			if(AppData.getInstance().playerCenterDataManager.fieldPlayerinjuredNum)
			{
				Alert.show("您的上场球员已经有5名受伤，球员们联合起来罢赛了！" +
					"他们抗议如此赤果果的压榨行为并且上报国际足联，老板，赶快去给他们疗伤吧~",
					"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,openPlayerCenter);
				return;
			}
			reqParam = e.param;
			if(AppData.getInstance().playerCenterDataManager.fieldisinjured)
			{
				hospSure(championMatchStart);
				return;
			}
			championMatchStart();
		}
		/**
		 *欧冠赛开始 
		 * @param flag
		 * 
		 */		
		private function championMatchStart(flag :int = Alert.YES):void
		{
			if(flag == Alert.YES)
			{
				this.sendNotification(ApplicationConstants.LEAGUEMATCH_CHAMPION_START,reqParam);
			}
			else
			{
				view.errorRes();
			}
		}
		/**
		 *打开球员中心 
		 * @param type
		 * 
		 */		
		private function openPlayerCenter(type:int):void
		{
			if(type == Alert.YES)
			{
				AppEventDispatcher.getInstance().dispatchEvent(new MainEvent(MainEvent.OPEN_PLAYERCENTER));
			}
			view.errorRes();
		}
		/**
		 *传奇赛开始 
		 * @param e
		 * 
		 */		
		private function legendMatchStartHandler(e :LeagueMatchEvent):void
		{
			if(AppData.getInstance().playerCenterDataManager.fieldPlayerinjuredNum)
			{
				Alert.show("您的上场球员已经有5名受伤，球员们联合起来罢赛了！" +
					"他们抗议如此赤果果的压榨行为并且上报国际足联，老板，赶快去给他们疗伤吧~",
					"",Alert.YES | Alert.NO,GlobalDef.AlertContainer,openPlayerCenter);
				return;
			}
			reqParam = e.param;
			if(AppData.getInstance().playerCenterDataManager.fieldisinjured)
			{
				hospSure(legendMatchStart);
				return;
			}
			legendMatchStart();
		}
		/**
		 *传奇赛开始 
		 * @param flag
		 * 
		 */		
		private function legendMatchStart(flag :int = Alert.YES):void
		{
			if(flag == Alert.YES)
			{
				this.sendNotification(ApplicationConstants.LEAGUEMATCH_LEGEND_START,reqParam);
			}
			else
			{
				view.errorRes();
			}
		}
		/**
		 *受伤判断 
		 * @param exCall
		 * 
		 */		
		private function hospSure(exCall :Function):void
		{
			Alert.show("您当前的队伍中有球员受伤，会大大影响球队的实力！\n您可以在球员中心治疗受伤的球员！\n是否继续比赛?","",Alert.NO,GlobalDef.AlertContainer,exCall);
		}
		/**
		 *签约 
		 * @param e
		 * 
		 */		
		private function signHandler(e :LeagueMatchEvent):void
		{
			this.sendNotification(ApplicationConstants.LEAGUEMATCH_SIGN_STAR,e.param);
		}
		/**
		 * 监听
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [LeagueMatchConst.MATCH_RESULT].concat(super.listNotificationInterests());
		}
		/**
		 * 捕获
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification :INotification):void
		{
			switch(notification.getName())
			{
				case NotificationConstants.MATCH_START://比赛开始
				{
					//viewComponent["mouseChildren"] = false;
					//viewComponent["mouseEnabled"] = false;
					break;
				}
				case LeagueMatchConst.MATCH_RESULT://开始返回
				{
					if(notification.getBody().retcode != 0 && view != null)
					{
						view.errorRes();
					}
					break;
				}
				case NotificationConstants.MATCH_END://比赛结束
				{
					if(view)
					{
						view.matchEnd();
					}
					break;
				}
			}
			
		}
		/**
		 * 注册
		 * 
		 */		
		override public function onRegister():void
		{
			//界面处理
			if(ResourceManager.instance.isContainResource(ResourceConst.LEAGUEMATCH_RES))
			{
				showUI();
				//@lake 2013-11-14 guide use
				TweenLite.delayedCall(.1,function():void{AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MODULE_LOAD_RES_COMPLETE);});
			}else
			{
				loader = new QueueLoader();
				var config:XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).leagueMatch[0];
				bytesTotal = int(config.@totalBytes);
				var releaseVersion :String;
				for each(var item:XML in config.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				
				loadRes();	
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			showUI();
			ResourceManager.instance.putResource(ResourceConst.LEAGUEMATCH_RES,resList);
			super.onLoadComplete(e);
		}
		/**
		 * 显示界面 
		 * 
		 */
		private function showUI():void
		{
			var ntype :int = AppData.getInstance().leagueMatchManager.ntype;
			if(ntype == LeagueMatchType.PRE_MATCH)
			{
				this.sendNotification(ApplicationConstants.LEAGUEMATCH_GET_PRE_INFO);
			}
			else if(ntype == LeagueMatchType.BASE_MATCH)
			{
				this.sendNotification(ApplicationConstants.LEAGUEMATCH_GET_BASE_INFO);
				this.sendNotification(ApplicationConstants.LEAGUEMATCH_GET_RELATION);
			}
			else if(ntype == LeagueMatchType.CHAMPION_MATCH)
			{
				this.sendNotification(ApplicationConstants.LEAGUEMATCH_GET_CHAMPION_INFO);
			}
			viewComponent = Reflection.createInstance("com.lz.football.module.leagueMatch.view.LeagueMatchView");
			view.initUI();
			PopUpManager.addPopUp(view as DisplayObject,GlobalDef.GameWindowContainer,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageWidth,false);
			addEventListeners();
			//通知进入比赛二级场景
			this.sendNotification(NotificationConstants.MATCH_ENTER_SECOND_SCENE);
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			view.destroyView();
			viewComponent = null;
		}
	}
}